NumPy Array Basics

In NumPy, an array is of type ndarray (n-dimensional array). A NumPy array is an array of homogeneous values (all of the same type), and all items occupy a contiguous block of memory.

To use NumPy, you first need to import the numpy package:


In [1]:
import numpy as np

Array operations are very similar to that of the Python list. For example, the following code snippet creates a Python list and then converts it to a NumPy array:


In [2]:
l1 = [1,2,3,4,5]
array1 = np.array(l1) # rank 1 array

In this case, array1 is known as a rank 1 (one dimensional) array. You can print out the array as usual using the print() function:


In [3]:
print (array1) # [1 2 3 4 5]


[1 2 3 4 5]

You can print out the shape of the array using the shape property:


In [4]:
print (array1.shape) # (5,)


(5,)

The shape property returns a tuple containing the dimension of the array. In the above example, array1 is a 1-dimensional array of five items.

Just like Python list, you can access items in the NumPy array using indexing as well as slicing:


In [5]:
print ('array1:', array1) # [1 2 3 4 5]
print ('array1.shape: ', array1.shape) # (5,)
print ('array1[0]:', array1[0]) # 1
print ('array1[1]:', array1[1]) # 2
print ('array1[1:3]:', array1[1:3]) # [2 3]
print ('array1[:-2]:', array1[:-2]) # [1 2 3]
print ('array1[3:]', array1[3:]) # [4 5]


array1: [1 2 3 4 5]
array1.shape:  (5,)
array1[0]: 1
array1[1]: 2
array1[1:3]: [2 3]
array1[:-2]: [1 2 3]
array1[3:] [4 5]

You can also pass in a list containing the index of the items you want to extract to the array:


In [6]:
print ('array1[[2,3]]:', array1[[2,3]]) # [3,4]


array1[[2,3]]: [3 4]

The following code snippet shows how you can create a two-dimensional array:


In [7]:
l2 = [6,7,8,9,0]
array2 = np.array([l1,l2]) # rank 2 array
print ('array2:', array2)
'''
[[1 2 3 4 5]
 [6 7 8 9 0]]
'''
print ('shape:', array2.shape) # (2,5) - 2 rows and
                            # 5 columns
print ('array2[0,0]:', array2[0,0]) # 1
print ('array2[0,1]:', array2[0,1]) # 2
print ('array2[1,0]:', array2[1,0]) # 6


array2: [[1 2 3 4 5]
 [6 7 8 9 0]]
shape: (2, 5)
array2[0,0]: 1
array2[0,1]: 2
array2[1,0]: 6

Creating and Initializing Arrays using NumPy

NumPy contains a number of helper functions that make it easy to initialize arrays with some default values. For example, if you want to create an array containing all zeroes, you can use the zeros() function with a number indicating the size of the array:


In [8]:
a1 = np.zeros(2) # array of rank 1 with all 0s
print ('a1.shape:', a1.shape) # (2,)
print ('a1[0]:', a1[0]) # 0.0
print ('a1[1]:', a1[1]) # 0.0


a1.shape: (2,)
a1[0]: 0.0
a1[1]: 0.0

If you want to create a rank 2 array, simply pass in a tuple:


In [9]:
a2 = np.zeros((2,3)) # array of rank 2 with all 0s;
                     # 2 rows and 3 columns
print ('a2.shape:', a2.shape) # (2,3)
print ('a2:', a2)


a2.shape: (2, 3)
a2: [[ 0.  0.  0.]
 [ 0.  0.  0.]]

To initialize the array to some other values other than zeroes, use the full() function:


In [16]:
a3 = np.full((2,3), 8.0) # array of rank 2
                           # with all 8s
print ('a3:', a3)


a3: [[ 8.  8.  8.]
 [ 8.  8.  8.]]

In [ ]:

In linear algebra, you often need to deal with an identity matrix, and you can create this in NumPy easily with the eye() function:


In [11]:
a4 = np.eye(4) # 4x4 identity matrix
print ('a4:', a4)


a4: [[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]

And if you need to populate an array with some random values, you can use the random.random() function to generate random values between 0.0 and 1.0:


In [12]:
a5 = np.random.random((2,4)) # populate a rank 2
                             # array (2 rows
                             # 4 columns) with
                             # random values
print ('a5:', a5)


a5: [[ 0.80902363  0.88658981  0.73351191  0.7686284 ]
 [ 0.73578443  0.97378247  0.99004091  0.6710785 ]]

Finally, you can create a range of values from 0 to n-1 using the arange() function:


In [13]:
a6 = np.arange(10) # creates a range from 0 to 9
print ('a6:', a6) # [0 1 2 3 4 5 6 7 8 9]


a6: [0 1 2 3 4 5 6 7 8 9]

Boolean Array Indexing

One of the many useful features of the NumPy array is its support for array boolean indexing. Consider the following example: You have a list of numbers and you need to retrieve all of the even numbers from the list. Using Python’s list, you need to iterate through all items in the list and perform a check individually. Using NumPy array, however, things are much easier. Look at the following example:


In [15]:
nums = np.array([23,45,78,89,23,11,22])

You first specify the condition, testing for even numbers, and then assign it to a variable:


In [19]:
even_nums = nums % 2 == 0

The even_nums variable is now a NumPy array, containing a collection of Boolean values. If you print it out, you’ll see just that:


In [20]:
print (even_nums)


[False False  True False False False  True]

The True values indicate that the particular item is an even number. Using this Boolean array, you can now use it as an index to the numbers array:


In [24]:
print (nums[even_nums])


[78 22]

The above statements could be written succinctly like this:


In [25]:
print (nums[nums % 2 == 0])


[78 22]

The array boolean indexing feature makes it extremely useful to manipulate a large amount of data without worrying about the underlying implementation. Here’s another example:


In [ ]: